package com.azkj.album.utils;

import android.text.Editable;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;

import com.azkj.album.MyApplication;
import com.azkj.album.R;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/*
    设置输入内容小数点后面只能读取两位
 */
public class NumberUtil {
    private static final Pattern NUMBER_PATTERN = Pattern.compile("-?\\d+(\\.\\d+)?");

    private static final int SPACE = 60;

    public static boolean isNumeric(String str) {
        if (TextUtils.isEmpty(str)) return false;
        return str != null && NUMBER_PATTERN.matcher(str).matches();
    }

    public static void setPricePoint(final EditText editText) {
        editText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                if (s.toString().contains(".")) {
                    if (s.length() - 1 - s.toString().indexOf(".") > 2) {
                        s = s.toString().subSequence(0,
                                s.toString().indexOf(".") + 3);
                        editText.setText(s);
                        editText.setSelection(s.length());
                    }
                }
                if (s.toString().trim().substring(0).equals(".")) {
                    s = "0" + s;
                    editText.setText(s);
                    editText.setSelection(2);
                }

                if (s.toString().startsWith("0")
                        && s.toString().trim().length() > 1) {
                    if (!s.toString().substring(1, 2).equals(".")) {
                        editText.setText(s.subSequence(0, 1));
                        editText.setSelection(1);
                        return;
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {

            }

        });

    }

    /**
     * 将数据保留两位小数
     */
    public static double getTwoDecimal(double num) {
        DecimalFormat dFormat = new DecimalFormat("#.00");
        String yearString = dFormat.format(num);
        Double temp = Double.valueOf(yearString);
        return temp;
    }


    /**
     * 小数点后为全0时去除
     */
    public static String formatNumber(double num) {
        if (num == 0) return "0";
        BigDecimal b = new BigDecimal(num);
        return formatNumber(b.setScale(2, BigDecimal.ROUND_HALF_UP).toString());
    }

    /**
     * 小数点后为全0时去除
     */
    public static String formatNumber(String num) {
        if (TextUtils.isEmpty(num) || !num.contains(".")) return num;
        if (num.endsWith(".0")) return num.replace(".0", "");
        if (num.endsWith(".00")) return num.replace(".00", "");
        if (num.endsWith(".000")) return num.replace(".000", "");
        if (num.endsWith(".0000")) return num.replace(".0000", "");
        if (num.endsWith(".00000")) return num.replace(".00000", "");
        if (num.endsWith("0000")) return num.substring(0, num.length() - 4);
        if (num.endsWith("000")) return num.substring(0, num.length() - 3);
        if (num.endsWith("00")) return num.substring(0, num.length() - 2);
        if (num.endsWith("0")) return num.substring(0, num.length() - 1);

        return num;
    }

    /**
     * 总金额 保留3位小数
     */
    public static String formatTotalMoney(String num) {

        if (TextUtils.isEmpty(num)) return "";

        try {
            double d = Double.parseDouble(num);
            if (d == 0) return null;
            String s = String.format("%.3f", d);
            return formatNumber(s);

        } catch (Exception e) {
            return num;
        }
    }

    /**
     * 四舍五入 取整
     *
     * @param num
     * @return
     */
    public static String getRoundNum(String num) {


        if (TextUtils.isEmpty(num) || !num.contains(".")) return num;
        try {
            float f = Float.parseFloat(num);
            return String.valueOf(Math.round(f));

//            BigDecimal b = new BigDecimal(num);
//            return formatNumber(b.setScale(0, BigDecimal.ROUND_HALF_UP).toString());
        } catch (Exception e) {
            return num;
        }


    }


    public static List<Integer> getTextWidth(List<List<String>> list, float Size) {

        List<Integer> result = new ArrayList<>();
        if (list == null || list.isEmpty() || list.get(0) == null || list.get(0).isEmpty())
            return result;

        int maxColumn = getMaxColumn(list);

        for (int i = 0; i < maxColumn; i++) {
            int max = 0;
            for (List<String> ll : list) {
                if (ll.size() > i) {
                    int temp = getTextWidth(ll.get(i), Size);
                    if (max < temp) max = temp;
                }
            }
            result.add(max);
        }
        return result;
    }


    public static int getTextWidth(String text, float Size) {
        if (TextUtils.isEmpty(text)) return 0;
        TextPaint paint = new TextPaint();
        paint.setTextSize(Size);
        return Math.max((int) paint.measureText(text) + SPACE, getMinTextWidth());

    }

    public static int getTextBoldWidth(String text, float Size) {
        if (TextUtils.isEmpty(text)) return 0;
        TextPaint paint = new TextPaint();
        paint.setFakeBoldText(true);
        paint.setTextSize(Size);
        return Math.max((int) paint.measureText(text) + SPACE, getMinTextWidth());

    }


    public static int getMaxTextWidth(List<String> list, float Size) {
        if (list == null || list.isEmpty()) return 0;
        int max = getMinTextWidth();
        for (String ss : list) {
            int temp = getTextWidth(ss, Size);
            if (max < temp) max = temp;
        }
        return max;
    }

    public static int getMaxTextBoldWidth(List<String> list, float Size) {
        if (list == null || list.isEmpty()) return 0;
        int max = getMinTextBoldWidth();
        for (String ss : list) {
            int temp = getTextBoldWidth(ss, Size);
            if (max < temp) max = temp;
        }
        return max;
    }


    public static <T> int getMaxColumn(List<List<T>> list) {
        int result = 0;
        if (list == null || list.isEmpty()) return result;
        for (List<T> ll : list) {

            if (ll != null && ll.size() > result) result = ll.size();
        }


        return result;
    }

    public static boolean isHasNumber(List<List<String>> list, int position) {
        if (list == null || list.isEmpty()) return false;
        for (List<String> ll : list) {
            if (ll.size() > position) {
                String content = ll.get(position);
                if (!TextUtils.isEmpty(content) && hasDigit(content)) {
                    return true;
                }
            }
        }


        return false;
    }


    /**
     * 判断一个字符是否包含数字
     * h
     *
     * @param content
     * @return
     */
    public static boolean hasDigit(String content) {
        boolean flag = false;
        Pattern p = Pattern.compile(".*\\d+.*");
        Matcher m = p.matcher(content);
        if (m.matches()) {
            flag = true;
        }
        return flag;
    }


    public static int getMinTextWidth() {
        TextPaint paint = new TextPaint();
        paint.setTextSize(MyApplication.getApplication().getResources().getDimension(R.dimen.sp_14));
        return (int) paint.measureText("商品名称") + SPACE;

    }

    public static int getMinTextBoldWidth() {
        TextPaint paint = new TextPaint();
        paint.setFakeBoldText(true);
        paint.setTextSize(MyApplication.getApplication().getResources().getDimension(R.dimen.sp_14));
        return (int) paint.measureText("商品名称") + SPACE;

    }


    public static List<Integer> getEditWidth(List<List<String>> list, float Size) {

        List<Integer> result = new ArrayList<>();
        if (list == null || list.isEmpty() || list.get(0) == null || list.get(0).isEmpty())
            return result;

        int maxColumn = getMaxColumn(list);

        for (int i = 0; i < maxColumn; i++) {
            int max = 0;
            for (List<String> ll : list) {
                if (ll.size() > i) {
                    int temp = getEditWidth(ll.get(i), Size);
                    if (max < temp) max = temp;
                }
            }
            result.add(max);
        }
        return result;
    }

    public static int getMinEditWidth() {
        TextPaint paint = new TextPaint();
        paint.setTextSize(MyApplication.getApplication().getResources().getDimension(R.dimen.sp_14));
        return (int) paint.measureText("商品名称商品") + SPACE;

    }

    public static int getEditWidth(String text, float Size) {
        if (TextUtils.isEmpty(text)) return 0;
        TextPaint paint = new TextPaint();
        paint.setTextSize(Size);
        return Math.max((int) paint.measureText(text) + SPACE, getMinEditWidth());

    }

    public static int getMaxEditWidth(List<String> list, float Size) {
        if (list == null || list.isEmpty()) return 0;
        int max = getMinEditWidth();
        for (String ss : list) {
            int temp = getEditWidth(ss, Size);
            if (max < temp) max = temp;
        }
        return max;
    }


    //两数乘法,保留n位小数
    public static String multiply(String n1, String n2, int point) {
        if (TextUtils.isEmpty(n1) || TextUtils.isEmpty(n2)) return null;
        try {
            BigDecimal b1 = new BigDecimal(n1);
            BigDecimal b2 = new BigDecimal(n2);
            BigDecimal total = b1.multiply(b2).setScale(point, BigDecimal.ROUND_HALF_UP);
            return total.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;

    }


    //两数除法,保留n位小数
    public static String divide(String n1, String n2, int point) {
        if (TextUtils.isEmpty(n1) || TextUtils.isEmpty(n2)) return null;
        try {
            BigDecimal b1 = new BigDecimal(n1);
            BigDecimal b2 = new BigDecimal(n2);
            BigDecimal total = b1.divide(b2, point, BigDecimal.ROUND_HALF_UP);
            return total.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;

    }

    public static int getMaxTextWidth8() {
        TextPaint paint = new TextPaint();
        paint.setTextSize(MyApplication.getApplication().getResources().getDimension(R.dimen.sp_14));
        return (int) paint.measureText("商品名称商品名称") + SPACE;

    }

    public static int getMaxTextWidth6() {
        TextPaint paint = new TextPaint();
        paint.setTextSize(MyApplication.getApplication().getResources().getDimension(R.dimen.sp_14));
        return (int) paint.measureText("商品名称商品") + SPACE;

    }

    public static int getMaxTextWidth6NoSpace() {
        TextPaint paint = new TextPaint();
        paint.setTextSize(MyApplication.getApplication().getResources().getDimension(R.dimen.sp_14));
        return (int) paint.measureText("商品名称商品") + 10;

    }

    public static int getMaxTextWidth12NoSpace() {
        TextPaint paint = new TextPaint();
        paint.setTextSize(MyApplication.getApplication().getResources().getDimension(R.dimen.sp_14));
        return (int) paint.measureText("商品名称商品名称商品名称") + 10;

    }

    public static int getMaxTextWidth8NoSpace() {
        TextPaint paint = new TextPaint();
        paint.setTextSize(MyApplication.getApplication().getResources().getDimension(R.dimen.sp_14));
        return (int) paint.measureText("商品名称商品商品") + 10;

    }

    public static boolean isEmptyOrZero(String num) {
        if (TextUtils.isEmpty(num)) return true;
        if ("0".equals(num) || "0.0".equals(num) || "0.00".equals(num) || "0.000".equals(num) || "0.0000".equals(num)
        ) return true;
        return false;

    }

}
